package edu.moravian.world;

import edu.moravian.math.Point2D;
import java.awt.Point;

/**
 *
 * @author Elliot
 */
public class CoordinateTranslator 
{
    private double worldWidth;
    private double worldHeight;
    private int screenWidth;
    private int screenHeight;
    
    /**
     * This class translates integer screen coordinates with the origin in the top left of the screen
     * to double world coordinates with the origin in the bottom left of the screen and vice versa
     * 
     * @param worldWidth the world width 
     * @param worldHeight the world height
     * @param screenWidth the screen width 
     * @param screenHeight the screen height
     */
    public CoordinateTranslator(double worldWidth, double worldHeight, int screenWidth, int screenHeight)
    {
        this.worldWidth = worldWidth;
        this.worldHeight = worldHeight;
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;
    }
    
    /**
     * returns the world point that is at the location of the screen point 
     * 
     * @param screenPoint the point to translate 
     * 
     * @return the translated world point 
     */
    public Point2D screenPointToWorldPoint(Point screenPoint)
    {
        //calculate the width and height ratio
        double xRatio = ((double)screenPoint.x / screenWidth);
        double yRatio = ((double)screenPoint.y / screenHeight);
        
        //calculate the world points if the origin was the same 
        //and then flip the y since the origin of the world is bottom left
        double xWorld = xRatio * worldWidth;
        double yWorld = worldHeight - (yRatio * worldHeight);
        
        return new Point2D(xWorld, yWorld);
    }
    
    
    /**
     * Returns the screen point that is at the same location of the world point
     * 
     * @param worldPoint the point to translate
     * 
     * @return the translated screen point 
     */
    public Point worldPointToScreenPoint(Point2D worldPoint)
    {
         //calculate the width and height ratio
        double xRatio = (worldPoint.getX() / worldWidth);
        double yRatio = (worldPoint.getY() / worldHeight);
        
        int xScreen = (int)(xRatio * screenWidth);
        int yScreen = screenHeight - ((int)(yRatio * screenHeight));
        
        return new Point(xScreen, yScreen);
    }
    
    /**
     * Returns the value of a horizontal scaler from the world to the screen 
     * 
     * @param worldScaler the scaler length in the world 
     * 
     * @return the scaler length on the screen
     */
    public int worldWidthScalerToScreenWidthScaler(double worldScaler)
    {
        double xRatio = (worldScaler / worldWidth);
        
        int screenScaler = (int)(xRatio * screenWidth);
        
        return screenScaler;
    }
    
    
    /**
     * Returns the value of a vertical scaler from the world to the screen 
     * 
     * @param worldScaler the scaler length in the world 
     * 
     * @return the scaler length on the screen
     */
    public int worldHeightScalerToScreenHeightScaler(double worldScaler)
    {
        double yRatio = (worldScaler / worldHeight);
        
        int screenScaler = (int)(yRatio * screenHeight);
        
        return screenScaler;
    }
    
    
}
